
package com.asm.slidingmenu;

import android.app.Fragment;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.asm.DBloader.ASM_DB;
import com.asm.DBloader.ASM_UsersettingsDS;
import com.asm.DBloader.ASM_fASEDS;
import com.asm.DBloader.ASM_fDaynPrdDetDS;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.listviewitems.BarChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.ChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.LineChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.PieChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.DemoBase;

import com.dspl.asmswd.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Demonstrates the use of charts inside a ListView. IMPORTANT: provide a
 * specific height attribute for the chart inside your listview-item
 * 
 * @author Philipp Jahoda
 */
public class ListViewMultiChartActivity extends Fragment 
{

	public ListViewMultiChartActivity()
	{
		
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
	{

        View rootView = inflater.inflate(R.layout.asm_fragment_home, container, false);
        ListView lv = (ListView)rootView.findViewById(R.id.listView1);
        TextView TX = (TextView)rootView.findViewById(R.id.textView1);
        TextView TX1 = (TextView)rootView.findViewById(R.id.textView2);
        TextView TX2 = (TextView)rootView.findViewById(R.id.textView3);
        
                ArrayList<ChartItem> list = new ArrayList<ChartItem>();
               
                ASM_fASEDS ds2=new ASM_fASEDS(getActivity());
                ASM_UsersettingsDS dse=new ASM_UsersettingsDS(getActivity());
                String ASMcode = dse.getASMCode().toString().trim();
                
                String ASMname = ds2.getasename(ASMcode);
                
                ArrayList<String> ASMname1 =ds2.getasename1(ASMcode);
                ArrayAdapter<String> categoriesAdapter3 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, ASMname1);
                TX1.setText("The Swadeshi Industrial Works PLC");
                TX.setText("ASM NAME :- "+categoriesAdapter3.getItem(0).toString());
                TX2.setText("Version 1.0.3");
                // 30 items
                for (int i = 0; i < 4; i++) {
                    
                    if(i % 4 == 0) 
                    {
                    	try {
                    		list.add(new LineChartItem(generateDataLine(i + 1),getActivity(). getApplicationContext()));
        				} 
                    	catch (Exception e) 
                    	{
        					// TODO: handle exception
        					Log.v("ERROR", e.toString());
        				}
                        
                    }
                    else if(i % 4 == 1) 
                    {
                        list.add(new BarChartItem(generateDataBar(i + 1), getActivity().getApplicationContext()));
                    } 
                    else if(i % 4 == 2) 
                    {
                        list.add(new PieChartItem(generateDataPie(i + 1), getActivity().getApplicationContext()));
                        
                    }
                  
                }
        
                ChartDataAdapter cda = new ChartDataAdapter(getActivity().getApplicationContext(), list);
                lv.setAdapter(cda);
		
        
        return rootView;
    }
    /** adapter that supports 3 different item types */
    private class ChartDataAdapter extends ArrayAdapter<ChartItem> {
        
        public ChartDataAdapter(Context context, List<ChartItem> objects) {
            super(context, 0, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getItem(position).getView(position, convertView, getContext());
        }
        
        @Override
        public int getItemViewType(int position) {           
            // return the views type
            return getItem(position).getItemType();
        }
        
        @Override
        public int getViewTypeCount() {
            return 3; // we have 3 different item-types
        }
    }
    
    /**
     * generates a random ChartData object with just one DataSet
     * 
     * @return
     */
    private LineData generateDataLine(int cnt) {

        ArrayList<Entry> e1 = new ArrayList<Entry>();

        for (int i = 0; i < 12; i++) {
            e1.add(new Entry((int) (Math.random() * 65) + 40, i));
        }

        LineDataSet d1 = new LineDataSet(e1, "Annual Sales " + cnt + ", (1)");
        d1.setLineWidth(3f);
        d1.setCircleSize(5f);
        d1.setHighLightColor(Color.rgb(244, 117, 117));
        
        ArrayList<Entry> e2 = new ArrayList<Entry>();

        for (int i = 0; i < 12; i++) {
            e2.add(new Entry(e1.get(i).getVal() - 30, i));
        }

        LineDataSet d2 = new LineDataSet(e2, "Annual Sales " + cnt + ", (2)");
        d2.setLineWidth(3f);
        d2.setCircleSize(5f);
        d2.setHighLightColor(Color.rgb(244, 117, 117));
        d2.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
        d2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]);
        
        ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
        sets.add(d1);
        sets.add(d2);
        
        LineData cd = new LineData(getMonths(), sets);
        return cd;
    }
    
    /**
     * generates a random ChartData object with just one DataSet
     * 
     * @return
     */
    private BarData generateDataBar(int cnt) {

        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();

        for (int i = 0; i < 12; i++) 
        {
            entries.add(new BarEntry((int) (Math.random() * 70) + 30, i));
        }

        BarDataSet d = new BarDataSet(entries, "Annual Sales " + cnt);
        d.setBarSpacePercent(20f);
        d.setColors(ColorTemplate.VORDIPLOM_COLORS);
        d.setHighLightAlpha(255);
        
        BarData cd = new BarData(getMonths(), d);
        return cd;
    }
    
    /**
     * generates a random ChartData object with just one DataSet
     * 
     * @return
     */
    private PieData generateDataPie(int cnt) {

        ArrayList<Entry> entries = new ArrayList<Entry>();

        for (int i = 0; i < 4; i++) {
            entries.add(new Entry((int) (Math.random() * 70) + 30, i));
        }

        PieDataSet d = new PieDataSet(entries, "");
        
        // space between slices
        d.setSliceSpace(5f);
        d.setColors(ColorTemplate.VORDIPLOM_COLORS);
        
        PieData cd = new PieData(getQuarters(), d);
        return cd;
    }
    
    private ArrayList<String> getQuarters() {
        
        ArrayList<String> q = new ArrayList<String>();
        q.add("1st Quarter");
        q.add("2nd Quarter");
        q.add("3rd Quarter");
        q.add("4th Quarter");
        
        return q;
    }

    private ArrayList<String> getMonths() {

        ArrayList<String> m = new ArrayList<String>();
        m.add("Jan");
        m.add("Feb");
        m.add("Mar");
        m.add("Apr");
        m.add("May");
        m.add("Jun");
        m.add("Jul");
        m.add("Aug");
        m.add("Sep");
        m.add("Okt");
        m.add("Nov");
        m.add("Dec");

        return m;
    }
}
